package com.lowkey.sqlTools;

public class Restrictions {
	private Restrictions r = null;
	
	private String condition;

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}
	
	public Restrictions eq(String column,Object value){
		String condition="";
		if(value instanceof String){
			condition=column+"='"+value+"'";
		}else{
			condition=column+"="+value;
		}
		r= new Restrictions();
		r.setCondition(condition);
		return r;
	}
	public Restrictions lt(String column,Object value){
		String condition="";
		if(value instanceof String){
			condition=column+"<'"+value+"'";
		}else{
			condition=column+"<"+value;
		}
		r= new Restrictions();
		r.setCondition(condition);
		return r;
	}
	public Restrictions le(String column,Object value){
		String condition="";
		if(value instanceof String){
			condition=column+"<='"+value+"'";
		}else{
			condition=column+"<="+value;
		}
		r= new Restrictions();
		r.setCondition(condition);
		return r;
	}
	public Restrictions gt(String column,Object value){
		String condition="";
		if(value instanceof String){
			condition=column+">'"+value+"'";
		}else{
			condition=column+">"+value;
		}
		r= new Restrictions();
		r.setCondition(condition);
		return r;
	}
	public Restrictions ge(String column,Object value){
		String condition="";
		if(value instanceof String){
			condition=column+">='"+value+"'";
		}else{
			condition=column+">="+value;
		}
		r= new Restrictions();
		r.setCondition(condition);
		return r;
	}
	public Restrictions in(String column,Object[] value){
		String condition=column+" in (";
		for(int i=0;i<value.length;i++){
			Object v = value[i];
			if(v instanceof String){
				condition +="'"+v+"',";
			}else{
				condition +=v+",";
			}
		}
		condition = condition.substring(0,condition.length()-1);
		condition +=")";
		r= new Restrictions();
		r.setCondition(condition);
		return r;
	}
	public Restrictions like(String column,Object value){
		String condition=column+" like ";
		if(value instanceof String){
			condition +="'%"+value+"%'";
		}else{
			condition +="%"+value+"%";
		}
		r= new Restrictions();
		r.setCondition(condition);
		return r;
	}
	public Restrictions between(String column,Object value1,Object value2){
		String condition=column+" between ";
		if(value1 instanceof String){
			condition+="'"+value1+"' and '"+value2+"'";
		}else{
			condition+=value1+" and "+value2;
		}
		r= new Restrictions();
		r.setCondition(condition);
		return r;
	}
	
	public static Restrictions and(Restrictions r1,Restrictions r2){
		String condition = r1.getCondition()+" and "+ r2.getCondition();
		Restrictions r = new Restrictions();
		r.setCondition(condition);
		return r;
	}
	public static Restrictions or(Restrictions r1,Restrictions r2){
		String condition = r1.getCondition()+" or "+ r2.getCondition();
		Restrictions r = new Restrictions();
		r.setCondition(condition);
		return r;
	}
	public static void main(String[] args) {
		Restrictions r = new Restrictions();
		Restrictions result = Restrictions.or(r.like("java","lowkey"), Restrictions.and(r.eq("bookname","aaa"), r.le("bookId", 1)));
		System.out.println(result.getCondition());
		
	}
}
